//
//  CategoryTableViewCell.m
//  礼物
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "CategoryTableViewCell.h"

@implementation CategoryTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSub];
    }
    return self;
}
-(void)createSub
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(WIDTH - self.tableView.frame.size.width, self.tableView.frame.size.height);
    //设置item的列间距
    flowLayout.minimumInteritemSpacing = 10*WSCALE;
    //设置item的行间距
    flowLayout.minimumLineSpacing = 10*HSCALE;
    //设置item的上,左,下,右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10*HSCALE, 10*WSCALE, 10*HSCALE, 10*WSCALE);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    NSLog(@"======%f",self.bounds.size.width);
    self.collectionV = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width*WSCALE-20, 0) collectionViewLayout:flowLayout];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.showsHorizontalScrollIndicator = NO;
    self.collectionV.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:self.collectionV];
   
    //注册cell
    [self.collectionV registerClass:[CategoryCollectionCell class] forCellWithReuseIdentifier:@"cell"];
 
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //点击每个频道进入相应详情
    CategoryModel *categoryModel = [self.categories.subcategories objectAtIndex:indexPath.row];
    NSLog(@"???%@",self.delegate);
    [self.delegate getCategoryMessage:categoryModel];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.categories.subcategories.count == 0)
    {
        return 1;
    }
        return self.categories.subcategories.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
        //直接从cell重用池里取
        CategoryCollectionCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//        cell.myDelegate = self;//成为代理人
        
        CategoryModel *categoryModel = [self.categories.subcategories objectAtIndex:indexPath.row];
        cell.category = categoryModel;
        
        return cell;
    
}
-(void)setCategories:(Categories *)categories
{
    _categories = categories;
    CGRect temp = self.collectionV.frame;
    if (self.categories.subcategories.count % 3 == 0) {
        temp.size.height =self.categories.subcategories.count / 3 *(80*WSCALE +30*HSCALE+10*HSCALE)+10;
    }else{
        temp.size.height =(self.categories.subcategories.count / 3+1) *(80*WSCALE +30*HSCALE+10*HSCALE)+10;
    }
    
    self.collectionV.frame = temp;

    [self.collectionV reloadData];
    NSLog(@"_categories.count = %f",self.collectionV.frame.size.height);
}
//设置每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"4??????????????????");
    return CGSizeMake(80*WSCALE, 80*WSCALE + 30*HSCALE);
}
@end
